package com.zipking.cloud.springbootmybatis.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;

public class LambdaTest2 {
    public static void main(String[] args) {
        /*consumer(25, new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println("消费了"+integer+"元");

            }
        });
        consumer(25,money-> System.out.println("money = " + money));
        String strHandler = strHandler("北京", s -> {
            return s;
        });
        System.out.println("strHandler = " + strHandler);
        String handler = strHandler("北京", new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s;
            }
        });
        System.out.println("handler = " + handler);*/
      /*  UnaryOperator<String> unaryOperator = greet -> greet + " Bob11!";
        System.out.println(unaryOperator.apply("Hello")); // Hello Jack!*/
        List<String> stringList = Arrays.asList("a", "b", "c", "d");
        handle(stringList, s -> {
            // 内部类参数，实现处理逻辑
            System.out.println("我可以对 s（" + s + "） 做任何事情，而且我还知道他是什么类型");
        });
        List<String> nameList = Arrays.asList("J3-baiqi", "shaheshagn", "sunwukong");
       /* List<Integer> nameLengthList = functionHandle(nameList, s->{

                System.out.println("处理逻辑....【" + s + "】");
                return s.length();


        });*/
        List<Integer> nameLengthList2 = functionHandle(nameList,String::length);
        System.out.println(nameLengthList2);
    }
    public static void consumer(int money, Consumer<Integer> func) {
        func.accept(money);
    }

    public static String strHandler(String str, Function<String,String> fun) {
        return fun.apply(str);
    }

    public static <T> void handle(List<T> list, Consumer<T> consumer) {
        for (T t : list) {
            // 调用处理方法
            System.out.println(t);
            consumer.accept(t);
        }
    }

    /**
     * 获取列表中，姓名的长度
     *
     * @param nameList 名字列表
     * @param function 名字长度列表
     * @return
     */
    public static List<Integer> functionHandle(List<String> nameList, Function<String, Integer> function) {
        List<Integer> nameLengthList = new ArrayList<>(nameList.size());
        nameList.forEach(name -> nameLengthList.add(function.apply(name)));
        return nameLengthList;
    }
}
